<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="title">欢迎注册</h2>
      <el-form :model="form" :rules="rules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password prefix-icon="Lock" />
        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="captcha">
          <div class="captcha-container">
            <el-input v-model="form.captcha" placeholder="请输入验证码" style="width: 60%" />
            <div class="captcha-code" @click="generateCaptcha">
              {{ captchaCode }}
            </div>
          </div>
        </el-form-item>

        <el-button type="primary" class="login-btn" :loading="loading" @click="handleRegister">
          立即注册
        </el-button>

        <div class="links">
          <RouterLink to="/login"><el-link type="info" :underline="false">返回登陆</el-link></RouterLink>

        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'

export default {
  data() {
    return {
      // 新增验证码相关数据
      captchaCode: '',
      form: {
        username: '',
        password: '',
        captcha: ''
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{6,16}$/, message: '6-16位字母数字组合', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value.toLowerCase() !== this.captchaCode.toLowerCase()) {
                callback(new Error('验证码不正确'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async handleRegister() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true
        // 这里调用登录接口
        // await loginAPI(this.form)
        const { code, data, message } = await register(this.form)
        if (code !== 200) {
          this.$message.error("用户已存在")
          this.loading = false
          return
        }
        this.$message.success('注册成功')
        this.$router.push('/login')
      } catch (error) {
        console.log('注册失败', error)
      } finally {
        this.loading = false
      }
    },
    // 生成4位随机验证码
    generateCaptcha() {
      const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      this.captchaCode = Array.from({ length: 4 },
        () => chars[Math.floor(Math.random() * chars.length)]).join('')
    }
  },
  mounted() {
    this.generateCaptcha(); // 页面加载时自动生成验证码
  },
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #b8c5ff 0%, #764ba2 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  color: #303133;
  margin-bottom: 30px;
  font-size: 24px;
  letter-spacing: 2px;
}

.login-form {
  .el-input__inner {
    height: 48px;
    line-height: 48px;
  }

  .el-input__prefix {
    display: flex;
    align-items: center;
    left: 15px;
  }
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  letter-spacing: 4px;
  margin-top: 10px;
  transition: all 0.3s;
}

.links {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.el-link+.el-link {
  margin-left: 10px;
}

/* 验证码样式 */
.captcha-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.captcha-code {
  width: 30%;
  height: 40px;
  background: #f0f2f5;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  letter-spacing: 2px;
  cursor: pointer;
  border-radius: 4px;
  user-select: none;
}
</style>